/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import java.sql.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import model.Category;
import model.Post;
import model.State;

/**
 *
 * @author martin
 */
@Path("rest")
public class RestController {
    @GET
    @Path("hello")
    @Produces("text/plain")
    public String hello() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("RestPointPressePU");
        EntityManager entityManager = factory.createEntityManager();
        EntityTransaction transaction = entityManager.getTransaction();
        Category category = new Category();
        State state = new State();
        category = entityManager.find(Category.class, 2);
        state = entityManager.find(State.class, 1);
        
        if (category!= null && state!=null) {
            System.out.println("Objet bien chargé!!");
            
        }
        Date date = new Date(9,8,1992);
        transaction.begin();
        Post post = new Post();
        post.setIdPost(3);
        post.setSubject("Sujet du service rest");
        post.setContent("Contenu du service rest");
        post.setWritedDate(date);
        post.setIdCategory(category);        
        post.setIdState(state);
        entityManager.persist(post);
        transaction.commit();
        
        entityManager.close();
        factory.close();
        
        return "hello :-)";
    } 
}
